<?php
class User_IndexController extends Zend_Controller_Action{	
	private $menu;	
	/*
	 * Created by: truonghm;
	 * Created date: 29/10/2010
	 * Function: khoi tao cac gia tri thiet lap cua form 
	 */
	function init()
	{
		$this->view->title = Zend_Registry::get('translate')->translate("Header title");
   		$this->view->headTitle($this->view->title, 'PREPEND');		
	}	
	/**
	 * @deprecated: Danh sach thanh vien
	 * @category: User
	 * @author: truonghm
	 * *@version: 1
	 */
	public function indexAction(){
		$translate=Zend_Registry::get('translate');
		//$viewport=Zend_Registry::get('viewport');
		$layout = new Ofx_Layout($this->getRequest());
		
		$session = Zend_Registry::get('session');		
		//print_r($session);
		
		$vn_pageSize = $session->config["page_size"];
		//echo $vn_pageSize."--";
		
		$cls_gridConfig= new Ofx_Controller_ExtjsGrid("userlist");
		
		$store = $cls_gridConfig->getStore();
		$store->setUrl('/user/list/')
		      ->setBaseParams(array("limit"=>$vn_pageSize))
		      ->attachListener("load",
								new PhpExt_Listener(PhpExt_Javascript::functionDef(null, 
									"myGrid.getSelectionModel().selectRow(0);", array("g")),
									null, 10)
								);
		
		$layout->setOutput($store->getJavascript(false, "store"));
		$layout->setOutput($store->load(new PhpExt_Data_StoreLoadOptions(array("start"=>0,"limit"=>$vn_pageSize))));
		
		$grid = $cls_gridConfig->getGrid();
		$grid->setAutoExpandColumn("username")
		     ->setTitle($translate->translate("User list"));
		     
		$layout->setOutput($grid->getJavascript(false, "myGrid"));
		 
		// Center Region		
		$center = new PhpExt_TabPanel();
	
		$tabLayout = new PhpExt_Layout_TabLayout();
		$tabLayout->setDeferredRender(true);	

		$center->setActiveTab(0)
		       ->setLayout($tabLayout)
		       ->addItem($grid);
		       
		//get Toolbal       
		$tb = $center->getTopToolbar();
		Ofx_Extjs::getToolbar($tb,$this->getRequest()->getModuleName());
		
		$tb->addSeparator("-");
		$tb->addTextItem(null,$translate->translate("Search"));
		$tb->addSpacer(2);
		$searchField = new PhpExtUx_App_SearchField();
		$searchField->setStore($store)
            		->setWidth(300);
        $tb->addItem(3, $searchField);
        
		$layout->setViewport($center);
		
		//cbo role group
		$cboReader = new PhpExt_Data_JsonReader();
		$cboReader->setRoot("rows");		
		$cboReader->addField(new PhpExt_Data_FieldConfigObject("id"));
		$cboReader->addField(new PhpExt_Data_FieldConfigObject("name"));
		$cboReader->addField(new PhpExt_Data_FieldConfigObject("description"));
		        
	    $cboStore = new PhpExt_Data_Store();
		$cboStore->setUrl('/user/role/list/')
			   	 ->setReader($cboReader);
			   	 
		$layout->setOutput($cboStore->getJavascript(false, "cboStore"));
		$layout->setOutput($cboStore->load());
			   	 
		$combo = PhpExt_Form_ComboBox::createComboBox("cboRole","Role group")		
				->setTemplate('<tpl for="."><div class="x-combo-list-item"><b>{name}</b><br>{description}</div></tpl>')
	            ->setValueField("id")
	            ->setDisplayField("name")
                ->setMode(PhpExt_Form_ComboBox::MODE_LOCAL)
	            ->setTriggerAction(PhpExt_Form_ComboBox::TRIGGER_ACTION_ALL)
	            ->setTypeAhead(true)
	            ->setHiddenName("role_id")
	            ->setSelectOnFocus(true)
	            ->setMultiSelect(true)	            
	            ->setEmptyText("Select a state...");
	    
		$combo->setStore($cboStore);
		
		//Begin from Edit user===========================================================
		
		$txtId = PhpExt_Form_Hidden::createHidden("id");
		
		$txtUsername = PhpExt_Form_TextField::createTextField("username",$translate->translate("Username"))->setAllowBlank(false);
					   $txtUsername->setEmptyText("Username")->setAllowBlank(false);
		$txtPassword = PhpExt_Form_PasswordField::createPasswordField("password",$translate->translate("Password"),"password");			   
		
		
		
		$cls = new Ofx_Controller_ExtjsForm();
		$editFrom = $cls->createFrom("user","editForm");
		$editFrom->setUrl("/user/update/")
				 ->addItem($txtId)
				 ->addItem($txtUsername,new PhpExt_Layout_AnchorLayoutData("90%"))
				 ->addItem($combo);
		$layout->setOutput($editFrom->getJavascript(false, "editFrom"));
				
				 
		$editWin = new PhpExt_Window();
		$editWin->setTitle($translate->translate("Edit user"))
				->setCloseAction('hide')
				->setModal(true)
				->setLayout(new PhpExt_Layout_FitLayout())
				->setWidth(500)				
				->setHeight(400)
				->setBorder(true)
       			->setMinWidth(500)
       			->setMinHeight(400);
						
  		$btnEdit = PhpExt_Button::createTextButton("Edit")  					
					 ->setHandler(PhpExt_Javascript::variable("btnEdit_Click"));
		$btnCancel = PhpExt_Button::createTextButton("Cancel")
					 ->setHandler(new PhpExt_Handler(PhpExt_Javascript::inlineStm("editWin.hide();")));

		$editWin->addButton($btnEdit);
		$editWin->addButton($btnCancel);
		$editWin->addItem($editFrom);	

		$layout->setOutput($editWin->getJavascript(false, "editWin"));
		
        //end form Edit user=========================================================
                   
		//Begin create form add user========================
		
		$cls = new Ofx_Controller_ExtjsForm();
		$addFrom = $cls->createFrom("user","addForm");
		$addFrom->setUrl("/user/insert/");
		
		//add field fix tren form
		$addFrom->addItem($txtUsername,new PhpExt_Layout_AnchorLayoutData("90%"))
				->addItem($txtPassword,new PhpExt_Layout_AnchorLayoutData("90%"))
				->addItem($combo);
				
		$layout->setOutput($addFrom->getJavascript(false, "addFrom"));
				
		$addWin = new PhpExt_Window();
		$addWin->setTitle($translate->translate("Add user"))
				->setCloseAction('hide')
				->setModal(true)
				->setLayout(new PhpExt_Layout_FitLayout())
       			->setBorder(true)
       			->setMinWidth(500)
       			->setMinHeight(400)
				->setWidth(500)				
				->setHeight(400);		
		
		$btnAdd = PhpExt_Button::createTextButton("Add")
					 ->setHandler(PhpExt_Javascript::variable("btnAdd_Click"));
					 				 
		$btnCancel = PhpExt_Button::createTextButton("Cancel")
					 ->setHandler(new PhpExt_Handler(PhpExt_Javascript::inlineStm("
					  	addFrom.getForm().reset(); 
					  	addWin.hide();")));
					 
		$addWin->addButton($btnAdd);
		$addWin->addButton($btnCancel);
		$addWin->addItem($addFrom);				

		$layout->setOutput($addWin->getJavascript(false, "addWin"));	
		
		//End create form add user========================
		
		$this->view->layout= Ofx_Extjs::onReady($layout->getLayout());	
		
	}
	/**
	 * 
	 * @param int $uid id user can lay thong tin
	 * @desc lay thong tin thanh vien
	 */
	public function primarykeyAction(){
		
		$uid = $this->_request->getParam ('uid');
		
		$business = new User_Models_Business_User();
		$result = $business->primarykey($uid);
		echo $result;		
		$this->_helper->layout->disableLayout();
	}
}